<?php

class Member {

    private $referrals_enc_key='Oi56TCVH@#%^&*TFf78@hey@2';
    
    public function create_member($member_details) {
        $db_obj = new DB();
        $table = 'member';
        $db_obj->insert($table, $member_details);
        return  $db_obj->getLastInsertId();
    }

    public function update_member($member_details, $member_id) {
        $db_obj = new DB();
        $table = 'member';
        $data = $member_details;
        $where = array("member_id =" . $member_id );
        $db_obj->update($table, $data, $where);
    }

    public function member_exist($member_email) {
        $db_obj = new DB();
        $get_member_email_sql = "SELECT member_id
                                     FROM member
                                    WHERE member_email='".$member_email."'";
        $db_obj->query($get_member_email_sql);
        if($db_obj->rowCount() > 0) {
            $member_values = $db_obj->getRow($get_member_email_sql);
            $extra_obj = new Extra();
            return $extra_obj->objectToArray($member_values);
        } else {
            return false;
        }
    }

    public function member_login_validate($un,$pw) {
        $db_obj = new DB();
        $get_member_email_sql = "SELECT *
                                     FROM member
                                    WHERE member_email='".$un."' and member_password='".$pw."'";
        //echo $get_member_email_sql;
        $db_obj->query($get_member_email_sql);
        if($db_obj->rowCount() > 0) {
            $member_values = $db_obj->getRow($get_member_email_sql);
            $extra_obj = new Extra();
            return $extra_obj->objectToArray($member_values);
        } else {
            return false;
        }
    }

    public function get_referrer_details($enc_member_id, $reffered_email=''){
        $en_key=  $this->referrals_enc_key;
        $enc = new Encryption();
        $dec_member_id=$enc->decrypt($en_key, $enc_member_id);
        $member_array=$this->get_member_details($dec_member_id);
        return $member_array;
        
        /*
        $db_obj = new DB();
        $get_member_refferel_sql = "SELECT count(*)
                                     FROM member
                                    WHERE member_id='".$dec_member_id."'";
        //echo "<br>sql1: ".$get_member_refferel_sql;
        $db_obj->query($get_member_refferel_sql);
        if($db_obj->rowCount()>0){
            $set_member_refferel_sql="SELECT * 
                                    FROM member_refferels 
                                    WHERE reffered_email='{$reffered_email}'";
                                    //echo "<br>sql2: ".$set_member_refferel_sql;                                    
            $db_obj->query($set_member_refferel_sql);
            if($db_obj->rowCount()==0){                
                $update_member_refferel_sql="insert into member_refferels(member_id, reffered_email)
                                                values('{$dec_member_id}', '{$reffered_email}')";
                                                //echo "<br>sql4: ".$update_member_refferel_sql;   
                $db_obj->query($update_member_refferel_sql);
                return 0;
                
            }  else {
                return -2;  //already reffered
            }
            
        }  else {
            return -1;  //invalid refferer
        }*/
    }
    
    public function get_member_details($member_id) {
        $db_obj = new DB();
        $get_member_details_sql = "SELECT *
                                     FROM member
                                    WHERE member_id='".$member_id."'";
      //  echo $get_member_email_sql;
        $db_obj->query($get_member_details_sql);
        if($db_obj->rowCount() > 0) {
            $member_values = $db_obj->getRow($get_member_details_sql);
            $extra_obj = new Extra();
            return $extra_obj->objectToArray($member_values);
        } else {
            return false;
        }
    }
    
    public function get_member_orders($member_id, $is_complete='') {
        $db_obj = new DB();
        if(!empty($is_complete) && $is_complete!='C'){
            $where=" and order_status!='C'";
        }else{
            if($is_complete=='C'){
                $where=" and order_status='C'";
            }
        }
        $get_member_orders_sql = "SELECT o.order_id, of.offer_id, m.member_id, p.payment_id, no_of_offer_vouchers, credits_redeemed,
                                        o.order_number, of.offer_title, of.offer_start_date, of.offer_end_date, offer_prefix,
                                        of.regular_amount, of.discount_amount, o.creation_date, o.order_status, p.amount_paid
                                    FROM offer_orders o
                                    INNER JOIN member m ON m.member_id = o.member_id
                                    INNER JOIN offer of ON o.offer_id = of.offer_id
                                    LEFT JOIN payment p ON p.member_id = m.member_id
                                    AND of.offer_id = p.offer_id
                                    WHERE m.member_id='".$member_id."' ".$where;
        // echo $get_member_orders_sql;
        $db_obj->query($get_member_orders_sql);
        if($db_obj->rowCount() > 0) {
            $member_orders = $db_obj->getResults($get_member_orders_sql);
            $extra_obj = new Extra();
            return $extra_obj->objectToArray($member_orders);
        } else {
            return false;
        }
    }

    public function update_member_credits($member_id,$amount=100, $operator='+') {
        $db_obj = new DB();
        if($amount>0){        
            $update_member_credits = "UPDATE `member` 
                                SET `pending_credit` = coalesce(pending_credit, 0)".$operator."".$amount."
                                WHERE member_id ='".$member_id."'";
            //echo $update_member_credits;
            $db_obj->query($update_member_credits);
            return true;
        }else{
            return false;
        }
    }
}
?>
